package com.snap.vseries.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.utils.GetTextWidthUtil;
import com.snap.vseries.utils.SizeUtils;

import java.util.List;

public class ExperimentProcessView extends LinearLayout {

    RelativeLayout processLayout;
    FrameLayout textFrameLayout;
    private List<String> experimentProcessList;
    private int commonColor;
    private Drawable gdStepCheck, gdStepNow, gdStepYet;
    private int step;
    /**
     * 每个阶段小圆点的直径
     */
    private int stepDotDiam = 32;
    /**
     * 每个阶段文本的字体size
     */
    private int stepTvSize = 12;
    /**
     * 每个阶段文本之间连线的高度
     */
    private int lineHeight = 2;
    private int dotPaddingLeft, dotPaddingRight;
    private int lineWidth;


    public ExperimentProcessView(Context context) {
        super(context);
        init();
    }

    public ExperimentProcessView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        stepDotDiam = (int) getContext().getResources().getDimension(R.dimen.new_experiment_dot);
        lineHeight = (int) SizeUtils.Dp2Px(getContext(), lineHeight);
        commonColor = ((Activity) getContext()).getResources().getColor(R.color.gray8);
        setOrientation(VERTICAL);
        createStepNowDotBg();
        createStepYetDotBg();
        createStepCheckDotBg();
    }

    private void createStepNowDotBg() {
        gdStepNow = ((Activity) getContext()).getResources().getDrawable(R.drawable.ic_step_now);

    }

    private void createStepYetDotBg() {
        gdStepYet = ((Activity) getContext()).getResources().getDrawable(R.drawable.ic_step_null);
    }

    private void createStepCheckDotBg() {
        gdStepCheck = ((Activity) getContext()).getResources().getDrawable(R.drawable.ic_step_check);
    }

    /**
     * 此方法需要在控件宽高确定后调用
     */
    public void render() {
        removeAllViews();
        if (step > (experimentProcessList.size() - 1)) {
            throw new RuntimeException("最大阶段为" + (experimentProcessList.size() - 1));
        }
        //进度条布局(包含线条和圆点)
        processLayout = new RelativeLayout(getContext());
        textFrameLayout = new FrameLayout(getContext());
        initDot();
        ViewGroup.LayoutParams dotLayoutParams = new ViewGroup.LayoutParams(stepDotDiam, stepDotDiam);
        FrameLayout.LayoutParams textLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //圆点集合
        LinearLayout dotLayout = new LinearLayout(getContext());
        ViewGroup.LayoutParams dotParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dotLayout.setOrientation(HORIZONTAL);
        dotLayout.setLayoutParams(dotParams);
        //线条集合
        LinearLayout lineLayout = new LinearLayout(getContext());
        ViewGroup.LayoutParams lineParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, stepDotDiam);
        lineLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        lineLayout.setOrientation(HORIZONTAL);
        lineLayout.setLayoutParams(lineParams);
        //添加顶部圆点和线条 ⚪-⚪-⚪，包含五个小控件
        for (int i = 0; i < experimentProcessList.size(); i++) {
            //添加上部分圆点
            View dotSuccessView = createDotView(dotLayoutParams);
            if (step == i) {
                dotSuccessView.setBackground(gdStepNow);
            } else if (step > i) {
                dotSuccessView.setBackground(gdStepCheck);
            } else {
                dotSuccessView.setBackground(gdStepYet);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(stepDotDiam, stepDotDiam);
            dotSuccessView.setLayoutParams(params);
            dotLayout.addView(dotSuccessView);
            //占位View
            if (i < experimentProcessList.size() - 1) {
                View view = new View(getContext());
                LayoutParams param = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);
                view.setLayoutParams(param);
                dotLayout.addView(view);
            }
            //添加上部分线条
            if (i < experimentProcessList.size() - 1) {
                View line = new View(getContext());
                if (i < step) {
                    line.setBackgroundColor(0xFF5699FC);
                } else {
                    line.setBackgroundColor(0xFFDFE2E9);
                }
                LinearLayout.LayoutParams lineLayoutParams = new LinearLayout.LayoutParams(lineWidth, lineHeight);
                lineLayoutParams.setMargins(0, (stepDotDiam - lineHeight) / 2, 0, 0);
                line.setLayoutParams(lineLayoutParams);
                lineLayout.addView(line);
            }
            //添加下部分文本
            TextView textView = new TextView(getContext());
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, stepTvSize);
            textView.setText(experimentProcessList.get(i));
            if (step == i) {
                textView.setTextColor(0xFF568BFF);
            } else {
                textView.setTextColor(commonColor);
            }
            if (i > 0) {
                int textPaddingLeft = dotPaddingLeft + stepDotDiam / 2 +
                        (getWidth() - getPaddingLeft() - getPaddingRight() - dotPaddingLeft - dotPaddingRight - stepDotDiam) / (experimentProcessList.size() - 1) * i
                        - GetTextWidthUtil.getTextWidth(textView.getPaint(), experimentProcessList.get(i)) / 2;
                textView.setPadding(textPaddingLeft, 0, 0, 0);
            }
            textFrameLayout.addView(textView, textLayoutParams);
        }
        processLayout.addView(lineLayout);
        processLayout.addView(dotLayout);
        addView(processLayout);
        LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(textFrameLayout, layoutParams);
    }

    /**
     * 创建一个圆点
     *
     * @param dotLayoutParams
     * @return
     */
    private View createDotView(ViewGroup.LayoutParams dotLayoutParams) {
        View dotView = new View(getContext());
        dotView.setLayoutParams(dotLayoutParams);
        return dotView;
    }

    /**
     * 初始化圆点和线条
     */
    private void initDot() {
        TextView textView = new TextView(getContext());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, stepTvSize);
        dotPaddingLeft = (GetTextWidthUtil.getTextWidth(textView.getPaint(), experimentProcessList.get(0)) - stepDotDiam) / 2;
        dotPaddingRight = (GetTextWidthUtil.getTextWidth(textView.getPaint(), experimentProcessList.get(experimentProcessList.size() - 1)) - stepDotDiam) / 2;
        lineWidth = (getWidth() - getPaddingLeft() - getPaddingRight() - stepDotDiam) / (experimentProcessList.size() - 1);
    }

    /**
     * 设置所有文本
     *
     * @param experimentProcessList
     * @return
     */
    public ExperimentProcessView setExperimentProcess(List<String> experimentProcessList) {
        if (experimentProcessList == null || experimentProcessList.size() < 2) {
            throw new RuntimeException("进度内容至少要有2条");
        }
        this.experimentProcessList = experimentProcessList;
        return this;
    }

    public ExperimentProcessView setStep(int step) {
        this.step = step;
        return this;
    }
}
